﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using CPPEI.Coolzon.Event.Core;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace CPPEI.Coolzon.Theme.Fashion.MainViewControl
{
    public partial class OperationNavigation
    {
        [Common.Subscribe]
        public void OnOpened(CoreOpenSceneCompletedEvent @event)
        {
            _sceneName = @event.SceneName;

            if (IsShowMagnifier)
            {
                IsShowMagnifier = false;
                magnifierImg.Style = (Style)dockPanel.Resources["magnifier"];
            }
            if (IsShowLaserPen)
            {
                IsShowLaserPen = false;
                laserPenImg.Style = (Style)dockPanel.Resources["laserPen"];
            }

            if (@event.SwitchMode == SwitchMode.Pop)
            {
                // 弹出场景，显示按钮交互界面
                ShowButtonInteractionMode();
                this.IsEnabled = false;
            }
            else
            {
                this.IsEnabled = true;
            }

            // 判断打开的场景和方案，是否要显示扩展按钮
            _ctlExtFuncContainer.Children.Clear();
            string solutionName = UIContext.Current.Solution.Name;
            string sceneName = @event.SceneName;
            for (int index = 0; index < ExtentionFunctionLoader.Instance.FunctionList.Count; index++)
            {
                ExtentionFunction func = ExtentionFunctionLoader.Instance.FunctionList[index];
                if (!string.IsNullOrEmpty(func.Filter))
                {
                    if(func.Filter.Contains(string.Format("@Solution={0}", solutionName))
                        &&func.Filter.Contains(string.Format("@Scene={0}", sceneName)))
                    {
                        //  显示自定义扩展按钮

                        Grid extBtn= new Grid();
                        extBtn.SetResourceReference(Button.StyleProperty, "gdFunc");
                      
                        extBtn.MouseLeftButtonDown += extBtn_MouseLeftButtonDown;
                        extBtn.Tag = func;
                        Image backImage = new Image();
                        backImage.Width = 24;
                        backImage.Height = 24;
                        backImage.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                        backImage.Margin = new Thickness(10, 0, 0, 0);
                        backImage.Source = new BitmapImage(new Uri("/CPPEI.Coolzon.Theme.Fashion;component/Images/4.png", UriKind.Relative));
                        extBtn.Children.Add(backImage);
                        TextBlock textBlock = new TextBlock();
                        textBlock.Text = func.Caption;
                        textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                        textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                        textBlock.Foreground = new SolidColorBrush(Colors.White);
                        textBlock.FontFamily = new FontFamily("微软雅黑");
                         textBlock.FontWeight = FontWeights.Bold;
                        textBlock.FontSize = 18;
                        textBlock.Margin = new Thickness(5);
                        extBtn.Children.Add(textBlock);

                        _ctlExtFuncContainer.Children.Add(extBtn);
                    }
                }
            }
        }

        void extBtn_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ExtentionFunction function = (sender as Grid).Tag as ExtentionFunction;
            if (string.Equals(function.Mode, "ExternalProgram", StringComparison.CurrentCultureIgnoreCase))
            {
                // 调用外部程序
                try
                {
                    string filePath = function.Action;
                    if (filePath.StartsWith("."))
                    {
                        filePath = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, filePath);
                    }
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
                    if (fileInfo.Exists)
                    {
                        string domain = UIContext.Current.ControlDomainId;
                        string solutionName = UIContext.Current.Solution.Name;
                        string sceneName = (UIContext.Current.RenderView as UI.Views.RenderView).CurrentScene.Name;

                        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                        info.FileName = function.Action;
                        info.Arguments = string.Format("{0} /domain {1} /solution {2} /scene {3}", function.Arguments,
                            domain,
                            solutionName,
                            sceneName);
                        info.Verb = "runas";
                        //info.UseShellExecute = false;
                        //info.WorkingDirectory = fileInfo.Directory.FullName;
                        System.Diagnostics.Process.Start(info);
                        Coolzon.Common.Logger.DebugToTag("UI", "run ExternalProgram {0} {1}", function.Arguments, function.Arguments);
                    }
                    else
                    {
                        Coolzon.Common.Logger.ErrorToTag("UI", "run ExternalProgram {0} {1}失败，文件不存在",
                            function.Action,
                            function.Arguments);
                    }
                }
                catch (Exception ex)
                {
                    Coolzon.Common.Logger.ErrorToTag("UI", "run ExternalProgram {0} {1}, ex:{2}",
                        function.Action,
                        function.Arguments,
                        ex);
                }
            }
        }
    }
}
